package com.bungarus.config;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import java.util.Arrays;
import java.util.Collection;

/**
 *
 * Created by tang on 2018/11/11.
 */
@Configuration
@PropertySource("application.properties")
@ComponentScan({"com.bungarus.model","com.bungarus.busi"})
@EnableMongoRepositories("com.bungarus.model")
public class AccessSpringConfig extends AbstractMongoConfiguration {
    @Value("${mongo.db}")
    private String database;

    @Value("${mongo.host}")
    private String host;

    @Value("${mongo.username}")
    private String username;

    @Value("${mongo.password}")
    private String password;

    @Value("${mongo.port}")
    private int port;

    @Override
    protected String getDatabaseName() {
        return database;
    }

    @Override
    public MongoClient mongoClient() {
//        MongoClient client = new MongoClient(new ServerAddress(host, port),
//                MongoCredential.createPlainCredential(username, getDatabaseName(), password.toCharArray()),
        MongoClient client = new MongoClient(new ServerAddress(host, port),
                MongoClientOptions.builder()
                        .connectionsPerHost(10)
                        .connectTimeout(2 * 60 * 1000)
                        .heartbeatConnectTimeout(2 * 60 * 1000)
                        .build());
        return client;
    }

    @Override
    protected Collection<String> getMappingBasePackages() {
        return Arrays.asList("com.bungarus.model");
    }
}
